package com.eric.servicestudy;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.servicestudy.R;

public class BindServiceActivity extends AppCompatActivity {
    private Button bt_startservice;
    private Button bt_stopservice;

    private Button bt_startOnbind;
    private Button bt_stopOnbind;
    private Button bt_getService;
    /* 保持所启动的 Service 的 IBinder 对象 , 同时定义一个 ServiceConnection 对象 */
    private ServiceOnbind.MyBinder mbinder = new ServiceOnbind.MyBinder(); // 需要 Mybinder 是 static
    private ServiceConnection conn = new ServiceConnection() {

        /*
            Activity 与 Service 连接成功时 候的 回调
         */
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            System.out.println("------服务 连接------");
            mbinder = (ServiceOnbind.MyBinder) service;
        }

        /*
            Activity 与 Service 连接断开 时候的 回调
         */
        @Override
        public void onServiceDisconnected(ComponentName name) {
            System.out.println("------服务 断开------");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_service);
        Init();
        Init_ServiceOnbind();
    }

    public void Init_ServiceOnbind() {
        bt_startOnbind = (Button) findViewById(R.id.bt_startOnbindService);
        bt_stopOnbind = (Button) findViewById(R.id.bt_stopOnbindService);
        bt_getService = (Button) findViewById(R.id.bt_getService);

        final Intent intent = new Intent(this, ServiceOnbind.class);
        intent.setAction("com.zhang.BIND_SERVICE");

        bt_startOnbind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 绑定 service
                bindService(intent, conn, Service.BIND_AUTO_CREATE);
            }
        });

        bt_stopOnbind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 解除 service  绑定
                unbindService(conn);
            }
        });

        bt_getService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // makeText 会在 app 界面进行显示文本
                Toast.makeText(getApplicationContext(), "服务的 count 值为:"
                        + mbinder.getCount(), Toast.LENGTH_SHORT
                ).show();
                //System.out.println("服务的 count ：" + mbinder.getCount());
            }
        });

    }


    public void Init() {
        bt_startservice = (Button) findViewById(R.id.bt_startservice);
        bt_stopservice = (Button) findViewById(R.id.bt_stopservice);

        // 创建启动 service 的 intent 以及 intent 的属性
        final Intent intent = new Intent(this, Myservice.class);
        intent.setAction("com.example.servicestudy.MY_SERVICE");

        bt_startservice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startService(intent);
            }
        });


        bt_stopservice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopService(intent);
            }
        });

    }
}